package com.haier.bims.samples.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.haier.bims.frozenbox.bean.Frozenbox;
import com.haier.bims.samples.bean.DropDown;
import com.haier.bims.samples.bean.Samples;
import com.haier.bims.samples.dao.SamplesDAO;
import com.haier.bims.samples.service.ISampleService;
import com.haier.common.ResultUtils;

@Component
public class SampleServiceImpl implements ISampleService {
	
	private SamplesDAO samplesDAO;

	@Autowired
	public void setSamplesDAO(SamplesDAO samplesDAO) {
		this.samplesDAO = samplesDAO;
	}

	@Override
	public List<Samples> getSampleInfoByids(String ids) {
		// TODO Auto-generated method stub
		String[] idsarr = ids.split(",");
		return this.samplesDAO.getSampleInfoByids(Arrays.asList(idsarr));
	}

	@Override
	public String insertEachSample(String body) {
		// TODO Auto-generated method stub
		Map<String, Object> m = ResultUtils.getQueryParams(body);
		String x = "";
		String y = "";
		String typeId ="";
		float unitValue = 0;
		String unitTypeId ="";
		String parentId ="";
		String projectId ="";
		String sectionId ="";
		String patientId = "";
		
		String projectName = "";
		String sectionName = "";
		String patient = "";
		String type = "";
		String unitType = "";
		
		if(m.get("x")!=null){
			x = m.get("x").toString();
		}
		if(m.get("y")!=null){
			y = m.get("y").toString();
		}
		if(m.get("typeId")!=null){
			typeId = m.get("typeId").toString();
		}
		if(m.get("unitValue")!=null){
			unitValue = Float.parseFloat(m.get("unitValue").toString());
		}
		if(m.get("unitTypeId")!=null){
			unitTypeId = m.get("unitTypeId").toString();
		}
		if(m.get("parentId")!=null){
			parentId = m.get("parentId").toString();
		}
		if(m.get("projectId")!=null){
			projectId = m.get("projectId").toString();
		}
		if(m.get("sectionId")!=null){
			sectionId = m.get("sectionId").toString();
		}
		if(m.get("patientId")!=null){
			patientId = m.get("patientId").toString();
		}
		
			try {
				projectName = java.net.URLDecoder.decode(new String(m.get("project").toString().getBytes("iso-8859-1"),"utf-8"),"UTF-8");
				sectionName = java.net.URLDecoder.decode(new String(m.get("section").toString().getBytes("iso-8859-1"),"utf-8"),"UTF-8");  
				patient = java.net.URLDecoder.decode(new String(m.get("patient").toString().getBytes("iso-8859-1"),"utf-8"),"UTF-8");  
				type = java.net.URLDecoder.decode(new String(m.get("type").toString().getBytes("iso-8859-1"),"utf-8"),"UTF-8");  
				unitType = java.net.URLDecoder.decode(new String(m.get("unitType").toString().getBytes("iso-8859-1"),"utf-8"),"UTF-8"); 
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
			Frozenbox frozenbox = new Frozenbox();
			frozenbox.setId(parentId);
			frozenbox = this.samplesDAO.qryFreezeBox(frozenbox).get(0);
		Samples sample = new Samples();
		String samid = this.getSampleNextID();
		sample.setId(samid);
		sample.setX(x);
		sample.setY(y);
		sample.setParentId(parentId);
		sample.setTypeId(typeId);
		sample.setUnitValue(unitValue);
		sample.setUnitType(unitType);
		sample.setUnitTypeId(unitTypeId);
		sample.setProjectId(projectId);
		sample.setProjectName(projectName);
		sample.setPatientId(patientId);
		sample.setPatient(patient);
		sample.setSectionId("241");
		sample.setSectionName("内科");
/*		sample.setSectionId(sectionId);
		sample.setSectionName(sectionName);
*/		sample.setType(type);
		sample.setHospitalId("41");
		sample.setCreateByID("621");
		sample.setCreateBy("RJadmin");
		sample.setFreezerId(frozenbox.getFreezerId());
		sample.setFreezerName(frozenbox.getFreezerName());
		sample.setFrozenShelfStorageId(frozenbox.getParentId());
		sample.setLineidentification(this.getRowAndCol(Integer.parseInt(x)-1));
		sample.setColidentification(y);
		this.samplesDAO.insertEachSample(sample);
		return samid;
	}
	
	@Override
	public String getSampleNextID(){
		return this.samplesDAO.getSampleNextID();
	}

	@Override
	public String updateEachSample(String body) {
		// TODO Auto-generated method stub
		Map<String, Object> m = ResultUtils.getQueryParams(body);
		String sampleId = m.get("sampleId").toString();
		String x = m.get("x").toString();
		String y = m.get("y").toString();
		String parentId = m.get("parentId").toString();
		Samples sample = new Samples();
		sample.setId(sampleId);
		sample.setParentId(parentId);
		sample.setX(x);
		sample.setY(y);
		sample.setLastModifiedByID("621");
		sample.setLastModifiedBy("RJadmin");
		sample.setLineidentification(this.getRowAndCol(Integer.parseInt(x)-1));
		sample.setColidentification(y);
		if(!"".equals(x) && !"0".equals(x)){
			sample.setState("0");
		}else{
			sample.setState("1");//出库
		}
		this.samplesDAO.updateEachSample(sample);
		String success = "1";
		return success;
	}

	
	@Override
	public List<DropDown> getDropDownListByTypes(String term) {
		// TODO Auto-generated method stub
		List<DropDown> list = new ArrayList<DropDown>();
		Samples sample = new Samples();
		sample.setType(term);
		sample.setHospitalId("41");
		list = this.samplesDAO.getDropDownListByType(sample);
		return list;
	}

	@Override
	public List<DropDown> getDropDownListByPatients(String term) {
		// TODO Auto-generated method stub
		List<DropDown> list = new ArrayList<DropDown>();
		Samples sample = new Samples();
		sample.setPatient(term);
		sample.setHospitalId("41");
		list = this.samplesDAO.getDropDownListByPatient(sample);
		return list;
	}

	@Override
	public List<DropDown> getDropDownListByProjects(String term) {
		// TODO Auto-generated method stub
		List<DropDown> list = new ArrayList<DropDown>();
		Samples sample = new Samples();
		sample.setProjectName(term);
		sample.setHospitalId("41");
		list = this.samplesDAO.getDropDownListByProject(sample);
		return list;
	}
	@Override
	public List<DropDown> getDropDownListByUnits() {
		// TODO Auto-generated method stub
		List<DropDown> list = new ArrayList<DropDown>();
		Samples sample = new Samples();
		sample.setHospitalId("41");
		list = this.samplesDAO.getDropDownListByUnit(sample);
		return list;
	}

	@Override
	public Map getObjectInfoByCode(String Code) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		Samples sample = new Samples();
		sample.setId(Code);
		Frozenbox frozenbox = new Frozenbox();
		frozenbox.setId(Code);
		List<Frozenbox> list_B = new ArrayList<Frozenbox>();
		List<Samples> list_S = new ArrayList<Samples>();
		//判断是盒子还是样本
		List<Samples> listS = this.samplesDAO.isSample(sample);
		if(listS.size()==0){
			list_B = this.samplesDAO.getFrozenbox(frozenbox);
			if(list_B.size()>0){
				map.put("type", "1001");
				map.put("data", list_B);
			}
			return map;
		}else{
			list_S = getSampleInfoByids(Code);
			if(list_S.size()>0){
				map.put("type", "1002");
				map.put("data", list_S);
			}
			return map;
		}
	}
	
	//获取样本对应的行列标识
	public String getRowAndCol(int i){
		String[] str = {"A","B","C","D","E","F","G","H","I"};
		return str[i].toString();
	}
	//样本ID获取
	public String getSampleNewIdByRules(){
		String Id = "";
		return Id;
	}
}
